<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */


/**
 * This class represents stylesheets. It cannot decorate other objects.
 * 
 * @category   Nordic
 * @package    Nordic_Pagebuilder
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: Stylesheet.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
class Nordic_Pagebuilder_Stylesheet extends Nordic_Pagebuilder_Decorated
{
	/**
	 * The active media for this stylesheet.
	 * 
	 * @var string
	 */
	private $_media = 'all';
	
	/**
	 * Media-types which are necessary for the css-files.
	 * 
	 * @var array
	 */
	private $_medias = array(
		'all',
		'aural',
		'braille',
		'embossed',
		'handheld',
		'print',
		'projection',
		'screen',
		'tty',
		'tv'
	);
	
	/**
	 * Name of the stylesheet.
	 * 
	 * @var string
	 */
	private $_name = '';
	
	/**
	 * Constructor.
	 * 
	 * Makes the objecet ready.
	 * 
	 * @param 
	 * @return void
	 */
	public function __construct ($name, $media = 'all')
	{
		$this->setMedia($media);
		$this->setName($name);
	}
	
	/**
	 * @see Nordic_Pagebuilder_Decorator::__toString()
	 */
	public function __toString ()
	{
		return 	'<link ' .
					'rel="stylesheet" ' .
					'type="' . $this->getType() . '" ' .
					'media="' . $this->getMedia() . '" ' .
					'href="' . ROOT_URI . $this->getName() . '" ' .
				'/>';
	}
	
	/**
	 * Returns the active media.
	 * 
	 * If the active media is not valid, all is set and returned.
	 * 
	 * @param void
	 * @return string
	 */
	public function getMedia ()
	{
		if(!$this->isValid($this->_media))
		{
			$this->setMedia('all');
			
			return $this->getMedia();
		}
		
		return $this->_media;
	}
	
	/**
	 * Returns the name of the stylesheet.
	 * 
	 * @param void
	 * @return string
	 */
	public function getName ()
	{
		return $this->_name;
	}
	
	/**
	 * Returns the type of this stylesheet.
	 * 
	 * @param void
	 * @return string
	 */
	public function getType ()
	{
		return 'text/css';
	}
	
	/**
	 * Returns if the media is valid.
	 * 
	 * @param string $media media to check
	 * @return boolean
	 */
	public function isValid ($media)
	{
		return in_array($media, $this->_medias);
	}
	
	/**
	 * Sets a new media. On success true is returned, otherwise false.
	 * 
	 * @param string $media new media
	 * @return boolean
	 */
	public function setMedia ($media)
	{
		if(!$this->isValid($media))
		{
			return false;
		}
		
		$this->_media = $media;
		
		return true;
	}
	
	/**
	 * Sets a new name for the css-file.
	 * 
	 * @param string $name name of the file
	 * @return void
	 */
	public function setName ($name)
	{
		if(Nordic_Util::getExtension($name) != 'css')
		{
			$name .= '.css';
		}
		
		$this->_name = $name;
	}
}
?>